<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/3/5
 * Time: 上午10:22
 */

namespace App\Admin\Controllers;


use App\Http\Controllers\Controller;
use App\Models\Dist;
use App\Models\Level;
use App\Models\SecQualifyCategory;
use App\Models\SupQualifyCategory;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use App\Busi\Traits\CategoryTrait;
use App\Busi\Traits\DistTrait;
class SecQualifycateController extends Controller
{
    use ModelForm,CategoryTrait,DistTrait;
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('资质二级分类列表');
            $content->description('');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('编辑二级分类');
            $content->description('');
            $content->body($this->form('edit')->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('添加二级分类');
            $content->description('');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid() {
        return Admin::grid(SecQualifyCategory::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $obj = $this;
            $grid->parent_cate_id('所属父类')->display(function ($parent_cate_id) use ($obj) {
                $parent_cate_name = $obj->getSupCateNameByID($parent_cate_id);

                return $parent_cate_name;
            });
            $grid->category_name('类别名称');
//            $grid->levels('关联等级')->display(function ($levels){
//
//                $levels = array_map(function ($level) {
//                    return "<a class='success' href='level/".$level['id']."' target='_blank'>{$level['name']}</a>";
//                }, $levels);
//
//                return join('--', $levels);
//            });
//            $grid->province_id("省份")->display(function ($province_id) use($obj){
//                $province_name = $obj->getDistNamePid($province_id);
//                return $province_name;
//            });
//            $grid->city_id("市")->display(function ($city_id) use($obj){
//                $city_name = $obj->getDistNamePid($city_id);
//                return $city_name;
//            });
            $grid->show_order('展示顺序');
            $grid->flag('是否生效')->display(function($flag){
                if( $flag==1 ) return '有效';
                return '无效';
            });
            $grid->updated_at('更新时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($mode='')
    {
        return Admin::form(SecQualifyCategory::class, function (Form $form) use($mode){
            if($mode=='edit'){
                $form->display('id','ID');
                $form->display('created_at', '创建时间');
                $form->display('updated_at', '更新时间');
            }
            $form->select('parent_cate_id', '父级类目')->options(function(){
                $data = SupQualifyCategory::pluck('category_name','id');
                $data = $data->toArray();
                return $data;
            });
            $form->text('category_name','类别名称');
            //$form->select("level","等级")->options([0=>"特级",1=>"一级",2=>"二级",3=>"三级"]);
//            $form->multipleSelect('levels','关联等级')->options(Level::all()->pluck('name', 'id'));
//            $form->text("detail_src","详情链接");
//            $form->radio("detail_src_from","链接来源")->options([0=>"内链",1=>"外链"])->default(0);
//            $form->select('province_id',"省")->options(Dist::where("parent_id",0)->pluck("dist_name","id"))->load('city_id', '/api/city');
//            $form->select('city_id',"市");




            $form->text('show_order','展示顺序,越小越靠前');
            $form->select('flag', '是否生效')->options([0=>'否', 1=>'是']);



        });
    }

}